﻿using System.Text.RegularExpressions;
using System;
using System.Web.Http;
using System.Collections.Generic;
using System.Linq;

using System.Threading.Tasks;
using Newtonsoft.Json;


namespace WindowsFormsApp.Controllers
{
    /// <summary>
    /// Home controller.
    /// </summary>
    [RoutePrefix("api")]
    public class HomeController : ApiController
    {
        /// <summary>
        /// Print the greetings
        /// </summary>
        /// <param name="name">visitor</param>
        /// <returns>greetings</returns>
        [Route("readRFTag")]
        [HttpGet]
        public IHttpActionResult readRFTag()
        {
            try
            {
                //Program.HFReader.startReading();
                string data = Program.HFReader.TryReadRFTag(2000);
                if (data != null && data != "")
                {
                    int num = Regex.Matches(data, "@").Count + 1;
                    //Program.HFReader.stopReading();
                    return Json(new { code = "200", data = data, num = $"{num}" });

                }
                return Json(new { code = "500", message = "读取失败,请重试" });
            } catch (Exception)
            {
                return Json(new { code = "500", message = "读取失败,请重试" });
            }
         
        }


        [Route("readRFTagMulti")]
        [HttpGet]
        public IHttpActionResult readRFTagMulti()
        {
            try
            {
                var readTimes = 5;
                List<dynamic> resultList = new List<dynamic>();
                for (int i = 0; i < readTimes; i++)
                {
                    string data = Program.HFReader.TryReadRFTag(2000);
                    if (data != null && data != "")
                    {
                        int num = Regex.Matches(data, "@").Count + 1;
                        resultList.Add(new { code = "200", data = data, num = $"{num}",message ="读取成功" });
                    }
                }
                if (resultList.Any())
                {
                    return Json(resultList);
                }
                resultList.Add(new { code = "500", message = "读取失败,请重试" });
                return Json(resultList);
            }
            catch (Exception)
            {
                return Json(new { code = "500", message = "读取失败,请重试" });
            }
        }


        // 异步方法，返回类型是Task<IHttpActionResult>
        [Route("readRFTagAsync")]
        [HttpGet]
        public async Task<IHttpActionResult> Get()
        {
            // 调用异步操作
            var data = await getDataAsync();
            return Ok(JsonConvert.DeserializeObject<Result>(data));
        }



        private async Task<string> getDataAsync()
        {
            return await Task.Run(() =>
            {
                string data = Program.HFReader.TryReadRFTag(1000);
                if (data != null && data != "")
                {
                    int num = Regex.Matches(data, "@").Count + 1;
                    return JsonConvert.SerializeObject(new Result("200", data, num, "读取成功"));
                }
                return JsonConvert.SerializeObject(new Result("200", data, 0, "读取成功"));
            });
        }

    }
}
